package com.yyx.qq.models;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 组 model
 * MyAndroidQQ
 * com.yyx.qq.models
 * Filename:    Group.java  
 * Description:   
 * Copyright:   Copyright (c)2009 
 * Company:     京北方科技股份有限公司  
 * @author:     朱磊  
 * @version:    1.0  
 * Create at:   2009-9-17 下午09:13:27  
 * @see
 */
public class Group extends Model {
	public String name;
	public List<User> users;
	public List<Cluster> clusters;
	public GroupType groupType;

	public Group() {
		super(Type.GROUP);
		initializeValues();
	}

	private void initializeValues() {
		name = EMPTY_STRING;
		users = new ArrayList<User>();
		clusters = new ArrayList<Cluster>();
		groupType = GroupType.FRIEND_GROUP;
	}

	public User getUser(int qq) {
		for (User u : users) {
			if (u.qq == qq)
				return u;
		}
		return null;
	}

	public boolean hasUser(int qq) {
		for (User u : users) {
			if (u.qq == qq)
				return true;
		}
		return false;
	}

	public boolean hasCluster(int id) {
		for (Cluster c : clusters) {
			if (c.clusterId == id)
				return true;
		}
		return false;
	}

	public Cluster getCluster(int id) {
		for (Cluster c : clusters) {
			if (c.clusterId == id)
				return c;
		}
		return null;
	}

	public int getOnlineUserCount() {
		int online = 0;
		for (User u : users) {
			if (!u.isOffline())
				online++;
		}
		return online;
	}

	public void removeUser(int index) {
		if (index < 0 || index >= users.size())
			return;
		User u = users.remove(index);
		u.group = null;
		ModelRegistry.removeUser(u);
	}

	public void addUser(User user) {
		if (hasUser(user.qq))
			return;
		users.add(user);
		user.group = this;
		ModelRegistry.addUser(user);
	}

	public void removeAllUsers() {
		for (Iterator<User> i = users.iterator(); i.hasNext();) {
			User u = i.next();
			u.group = null;
			i.remove();
			ModelRegistry.removeUser(u);
		}
	}

	public void removeAllCluster() {
		for (Iterator<Cluster> i = clusters.iterator(); i.hasNext();) {
			i.next().group = null;
			i.remove();
		}
	}

	public void removeUser(User user) {
		users.remove(user);
		user.group = null;
		ModelRegistry.removeUser(user);
	}

	/**
	 * 删除一个重复的用户
	 * 
	 * @param user
	 *            User对象
	 */
	public void removeDuplicate(User user) {
		users.remove(user);
		user.group = null;
	}

	public void addCluster(Cluster c) {
		c.group = this;
		if (clusters.contains(c))
			return;
		clusters.add(c);
	}

	public void removeCluster(Cluster c) {
		clusters.remove(c);
		c.group = null;
		ModelRegistry.removeCluster(c);
	}

	public int getUserCount() {
		return users.size();
	}

	public boolean isCluster() {
		return groupType == GroupType.CLUSTER_GROUP;
	}

	public boolean isFriendly() {
		switch (groupType) {
		case DEFAULT_FRIEND_GROUP:
		case FRIEND_GROUP:
			return true;
		default:
			return false;
		}
	}

	public boolean isDefaultFriend() {
		return groupType == GroupType.DEFAULT_FRIEND_GROUP;
	}

	public boolean isStranger() {
		return groupType == GroupType.STRANGER_GROUP;
	}

	public boolean isBlackList() {
		return groupType == GroupType.BLACKLIST_GROUP;
	}

	public boolean isLatest() {
		return groupType == GroupType.LATEST_GROUP;
	}

}
